package cn.edu.aust.zhuzhang.mydialogtest.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import java.util.Timer;
import java.util.TimerTask;

import cn.edu.aust.zhuzhang.mydialogtest.R;
import cn.edu.aust.zhuzhang.mydialogtest.View.MyProgressBar;

public class ProgressActivity extends AppCompatActivity {
    ProgressBar pb_default=null,pb_hori=null;
    MyProgressBar mpb_self=null;
    Button btn_defStart=null,
            btn_defEnd=null,
            btn_horiStart=null,
            btn_horiEnd=null,
            btn_selfStart=null,
            btn_selfEnd=null;
    int defCount=0,horiCount=0,selfCount=0;
    private Timer timer=null;
    private MyTimerTask timerTask=null;
    private MyTimerTask1 timerTask1=null;
    private TextView tv_horiShow=null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);

        btn_defStart=(Button)findViewById(R.id.btn_defStart);
        btn_defEnd=(Button)findViewById(R.id.btn_defEnd);
        btn_horiStart=(Button)findViewById(R.id.btn_horiStart);
        btn_horiEnd=(Button)findViewById(R.id.btn_horiEnd);
        btn_selfStart=(Button)findViewById(R.id.btn_selfStart);
        btn_selfEnd=(Button)findViewById(R.id.btn_selfEnd);

        pb_default=(ProgressBar)findViewById(R.id.pb_defalut);
        pb_default.setVisibility(View.INVISIBLE);
        pb_hori=(ProgressBar)findViewById(R.id.pb_hori);
        pb_hori.setVisibility(View.VISIBLE);
        mpb_self=(MyProgressBar)findViewById(R.id.pb_self);

        tv_horiShow=(TextView)findViewById(R.id.tv_horiShow);

        btn_defStart.setOnClickListener(listener);
        btn_defEnd.setOnClickListener(listener);
        btn_horiStart.setOnClickListener(listener);
        btn_horiEnd.setOnClickListener(listener);
        btn_selfStart.setOnClickListener(listener);
        btn_selfEnd.setOnClickListener(listener);
    }

    View.OnClickListener listener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_defStart:
                    pb_default.setVisibility(View.VISIBLE);
                    defCount=0;
                    if (timerTask!=null){
                        timerTask.cancel();
                    }

                    if (timerTask1!=null){
                        timerTask1.cancel();
                    }

                    if (timer==null){
                        timer= new Timer();
                    }
                    timerTask=new MyTimerTask(pb_default,null,defCount);
                    timer.schedule(timerTask, 0, 1000);
                    break;
                case R.id.btn_defEnd:
                    pb_default.setVisibility(View.INVISIBLE);
                    if (timer!=null){
                        timer.cancel();
                        timer=null;
                        timerTask.cancel();
                    }
                    defCount=0;
                    break;
                case R.id.btn_horiStart:
                    pb_hori.setVisibility(View.VISIBLE);
                    horiCount=0;

                    if (timerTask!=null){
                        timerTask.cancel();
                    }

                    if (timerTask1!=null){
                        timerTask1.cancel();
                    }

                    if (timer==null){
                        timer= new Timer();
                    }

                    timerTask=new MyTimerTask(pb_hori,tv_horiShow,horiCount++);
                    timer.schedule(timerTask, 0, 500);
                    break;
                case R.id.btn_horiEnd:
                    pb_hori.setVisibility(View.INVISIBLE);
                    if (timer!=null){
                        timer.cancel();
                        timer=null;
                        timerTask.cancel();
                    }
                    horiCount=0;
                    break;
                case R.id.btn_selfStart:
                    mpb_self.setVisibility(View.VISIBLE);
                    selfCount=0;
                    if (timerTask!=null){
                        timerTask.cancel();
                    }

                    if (timerTask1!=null){
                        timerTask1.cancel();
                    }

                    if (timer==null){
                        timer= new Timer();
                    }
                    timerTask1=new MyTimerTask1(mpb_self,selfCount++);
                    timer.schedule(timerTask1, 0, 500);
                    break;
                case R.id.btn_selfEnd:
//                    mpb_self.setVisibility(View.INVISIBLE);
                    if (timer!=null){
                        timer.cancel();
                        timer=null;
                        timerTask1.cancel();
                    }
                    selfCount=0;
                    break;
            }
        }
    };

    class MyTimerTask extends TimerTask{
        ProgressBar pbar;
        TextView tvShow;
        int count;
        MyTimerTask(ProgressBar pb, TextView show, int count){
            this.pbar=pb;
            this.tvShow=show;
            this.count=count;
        }

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (tvShow!=null&&count<=100){
                        tvShow.setText(count+"%");
                    }
                    if (count<=100){
                        pbar.setProgress(count++);
                    }else{
                        timer.cancel();
                    }
                }
            });
        }
    }

    class MyTimerTask1 extends TimerTask{
        MyProgressBar pbar;
        int count;
        MyTimerTask1(MyProgressBar pb, int count){
            this.pbar=pb;
            this.count=count;
        }

        @Override
        public void run() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (count<=100){
                        pbar.setProgress(count++);
                    }else{
                        timer.cancel();
                    }
                }
            });
        }
    }
}
